<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增加工镀铝')" />
    <!--<th:block th:include="include :: datetimepicker-css" />-->
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-aluminized-add">
            <div class="form-group">
                <label class="col-sm-3 control-label">合并材料编号：</label>
                <div class="col-sm-8">
                    <select name="ribbonId" id="ribbonId" class="form-control select2-multiple" onchange="getSurface(this.value)">
                        <option value="">请选择</option>
                        <option th:each="dict : ${ribbons}" th:text="${dict.ribbonNumber}" th:value="${dict.id}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-12">
                    <input type="hidden" name="inventoryIds" id="inventoryIds"/>
                    <label class="col-sm-3 control-label">原膜编号：</label>
                    <div class="col-sm-8">
                        <button type="button" class="btn btn-white btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>
                        <button type="button" class="btn btn-white btn-sm" onclick="delColumn()"><i class="fa fa-minus"> 删除</i></button>
                    </div>
                    <div class="col-sm-12 table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">镀铝面：</label>
                <div class="col-sm-8">
                    <input name="surface" id="surface" disabled class="form-control" type="hidden">
                    <input name="surfaceName" id="surfaceName" disabled class="form-control" type="text">
                    <!--<select name="surface" class="form-control select2-multiple"-->
                            <!--th:with="type=${@dict.getType('process_ribbon_state')}">-->
                        <!--<option value="">请选择</option>-->
                        <!--<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>-->
                    <!--</select>-->
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">质量文字反馈：</label>
                <div class="col-sm-8">
                    <input name="qualityState" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">质量图片反馈：</label>
                <div class="col-sm-4">
                    <input id="qualityImg" name="file" multiple type="file" data-show-caption="true">
                </div>
                <div class="col-sm-2">
                    <a class="btn btn-success" onclick='sendFileImg("qualityImg")'><i class="fa fa-plus">图片上传</i></a>
                </div>
                <div class="col-sm-1">
                    <a class="btn btn-warning" onclick='removeImg("qualityImg")'><i class="fa fa-plus">清空图片</i></a>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <div class="file-preview qualityImg" style="height: auto">
                    </div>
                    <input name="qualityImg" type="hidden">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">质量视频反馈：</label>
                <div class="col-sm-6">
                    <input id="qualityVideo" name="file" multiple type="file" data-show-caption="true">
                </div>
                <div class="col-sm-1">
                    <a class="btn btn-success" onclick='sendFile("qualityVideo")'><i class="fa fa-plus">视频上传</i></a>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <div class="file-preview qualityVideo" style="height: auto">
                    </div>
                    <input name="qualityVideo" type="hidden">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">操作起始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="startTime" id="startTime" class="form-control time-input" data-format="yyyy-MM-dd HH:mm:ss"
                               data-sign=3 placeholder="yyyy-MM-dd HH:mm:ss" data-type="datetime" autocomplete="off" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">抽真空结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="vacuoTime" id="vacuoTime" class="form-control time-input" data-format="yyyy-MM-dd HH:mm:ss"
                               data-sign=4 placeholder="yyyy-MM-dd HH:mm:ss" data-type="datetime" autocomplete="off" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">操作结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="endTime" id="endTime" class="form-control time-input" data-format="yyyy-MM-dd HH:mm:ss"
                               data-sign=5 placeholder="yyyy-MM-dd HH:mm:ss" data-type="datetime" autocomplete="off" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">抽真空耗时：</label>
                <div class="col-sm-7">
                    <input name="vacuoConsumeTime" id="vacuoConsumeTime" class="form-control" type="hidden" >
                    <input id="vacuoConsumeTimeBak" disabled class="form-control" type="text" >
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">分钟</label>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">总耗时：</label>
                <div class="col-sm-7">
                    <input name="consumeTime" id="consumeTime" class="form-control" type="hidden">
                    <input  id="consumeTimeBak" disabled class="form-control" type="text">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">分钟</label>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">起始电表度数：</label>
                <div class="col-sm-7">
                    <input name="ammeterStartTime" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">V</label>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">结束电表度数：</label>
                <div class="col-sm-7">
                    <input name="ammeterEndTime" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">V</label>
            </div>
            <div class="form-group">
                <input type="hidden" name="operatingEquipment" id="equaipment"/>
                <label class="col-sm-3 control-label">操作设备：</label>
                <div class="col-sm-8">
                    <select class="form-control" id="operat-select" onchange="chengeStatus(this.value)">
                        <option value="">请选择</option>
                        <option value="镀铝机">镀铝机</option>
                        <option value="南光机">南光机</option>
                        <option value="宝莱机">宝莱机</option>
                        <option value="其他">其他</option>
                    </select>
                    <input id="other" placeholder="输入设备" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">操作班次：</label>
                <div class="col-sm-8">
                    <input name="operatingNumber" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">操作人：</label>
                <div class="col-sm-8">
                    <!--<input name="operator" class="form-control" type="text" required>-->
                    <select name="operator" class="form-control" data-none-selected-text="请选择">
                        <option value="">请选择</option>
                        <option th:each="user : ${users}" th:text="${user.userName}" th:value="${user.userName}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">技术参数(选填):</label>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">镀膜速度：</label>
                <div class="col-sm-2">
                    <input name="speedBefore" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center"> - </label>
                <div class="col-sm-2">
                    <input name="speedAfter" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">m/min</label>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">铝层厚度：</label>
                <div class="col-sm-2">
                    <input name="thicknessBefore" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center"> - </label>
                <div class="col-sm-2">
                    <input name="thicknessAfter" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">Ω/□</label>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">送丝频率：</label>
                <div class="col-sm-2">
                    <input name="frequencyBefore" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center"> - </label>
                <div class="col-sm-2">
                    <input name="frequencyAfter" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">Hz</label>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">蒸发功率：</label>
                <div class="col-sm-2">
                    <input name="powerBefore" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center"> - </label>
                <div class="col-sm-2">
                    <input name="powerAfter" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">%</label>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">放卷张力：</label>
                <div class="col-sm-2">
                    <input name="unreelingBefore" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center"> - </label>
                <div class="col-sm-2">
                    <input name="unreelingAfter" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">N</label>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">收卷张力：</label>
                <div class="col-sm-2">
                    <input name="rollingBefore" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center"> - </label>
                <div class="col-sm-2">
                    <input name="rollingAfter" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">N</label>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">附加张力：</label>
                <div class="col-sm-2">
                    <input name="appendBefore" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center"> - </label>
                <div class="col-sm-2">
                    <input name="appendAfter" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">N</label>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">真空度G：</label>
                <div class="col-sm-2">
                    <input name="vacuumBefore" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center"> - </label>
                <div class="col-sm-2">
                    <input name="vacuumAfter" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">pa</label>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">真空度G5：</label>
                <div class="col-sm-2">
                    <input name="vacuum5Before" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center"> - </label>
                <div class="col-sm-2">
                    <input name="vacuum5After" placeholder="请输入" class="form-control" type="number">
                </div>
                <label class="col-sm-1 control-label" style="text-align:center">pa</label>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <!--<th:block th:include="include :: datetimepicker-js" />-->
    <th:block th:include="include :: select2-js" />
    <!--<th:block th:include="include :: bootstrap-select-js" />-->
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "process/aluminized"
        $("#form-aluminized-add").validate({
            focusCleanup: true
        });
        $(function () {
            $("#ribbonId").select2();
        });

        function submitHandler() {
            //原膜编号
            var table1 = $("#bootstrap-table").bootstrapTable('getData');
            var ids = table1.map(function (item) {
                return item.id;
            }).join(',');
            $("#inventoryIds").val(ids);
            //操作人
            var select = $("#operat-select").val();
            if(select == "其他"){
                $("#equaipment").val($("#other").val());
            }else{
                $("#equaipment").val(select);
            }
            
            if ($.validate.form()) {
            	 var  ff = $("#surfaceName").val();
            	 console.log(ff+"11111111")
                       var surface ;
                       if(ff == "内" ){
                    	   surface = 1 ;
                       }else if(ff =="外"){
                    	   surface = 2;
                       }else if( ff = "内+外" ){
                    	   surface = 3;
                       }
                    //   $("#surface").val(surface);
            	
                $.operate.save(prefix + "/add", $('#form-aluminized-add').serialize()+"&surface="+surface);
            }
        }
        //上传多个图片
        var imgs = [];
        function sendFileImg(name) {
            var formData = new FormData();
            //注入 name=file
            var files = $('#'+name)[0].files;
            if(files.length>10){
                layer.alert("上传上限位10");
                return false;
            }
            for (var i = 0; i < files.length; i++) {
                //注意：这里append进去的是File对象，而不是FileList对象
                formData.append("file", files[i]);
            }

            //  var formData = new FormData($('#'+name)[0]);
            // pp.append("file", val);
            $.ajax({
                type: "POST",
                url: ctx + "common/uploadNew",
                data: formData,
                processData: false,
                contentType: false,
                success: function(result) {
                    if (result!=null && result.code == web_status.SUCCESS) {
                        var a = result.url;
                        var p ="";
                        for (var i = 0; i < a.length; i++) {
                            imgs.push(a[i]);
                        }
                        for (var j = 0; j < imgs.length; j++) {
                            p=p+'<img src="'+imgs[j]+'" style="height: 100%;width: 100%">';
                        }
                        $('.'+name).html(p);
                        //$('[name="'+name+'"]').val(result.url);
                        $('[name="'+name+'"]').val(imgs.join(","));
                    } else {
                        $.modal.alertError("上传失败");
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败");
                }
            });
        }
        function removeImg(name) {
            imgs = [];
            $('.'+name).html("");
            $('#'+name).val("");
            $('[name="'+name+'"]').val("");
        }
        // 上传文件
        function sendFile(name) {
            var val = $('#'+name)[0].files;
            var indexOf = val[0].name.indexOf(".")+1;
            var s = val[0].name.slice(indexOf,val[0].name.length);
            var data = new FormData();
            data.append("file", val[0]);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result!=null && result.code == web_status.SUCCESS) {
                        if(s=="mp4"){
                            $('.'+name).html('<video src="'+result.url+'" controls="controls" style="height: 170px;width: 100%">');
                            $('[name="'+name+'"]').val(result.url);
                        }else{
                            $('.'+name).html('<img src="'+result.url+'" style="height: 100%;width: 100%">');
                            $('[name="'+name+'"]').val(result.url);
                        }
                    } else {
                        $.modal.alertError("上传失败");
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败");
                }
            });
        }
        function chengeStatus(val) {
            if(val == "其他"){
                $("#other").attr("type","text");
            }else{
                $("#other").attr("type","hidden");
            }
        }

        // --------------------------表格操作-----------------------------------
        var data;
        $(function() {
            var options = {
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: 'id',
                    visible: false
                },
                {
                    field: 'number',
                    align: 'center',
                    title: '原膜编号',
                    formatter: function(value, row, index) {
                        var obj = {};
                        obj.index = index;
                        obj.list = data;
                        return $("#goodsTypeTpl").tmpl(obj).html();
                    }
                },
                {
                    field: 'factoryName',
                    align: 'center',
                    title: '厂家',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' name='goods[%s].factoryName' disabled type='text' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'materialsName',
                    align: 'center',
                    title: '材料名称',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' name='goods[%s].materialsName' disabled type='text' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'color',
                    align: 'center',
                    title: '颜色',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' name='goods[%s].color' disabled type='text' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'mDate',
                    align: 'center',
                    title: '来料日期',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' name='goods[%s].mDate' disabled type='text' value='%s'>", index, value);
                        return html;
                    }
                }]
            };
            $.table.init(options);
        });
        function getData(ribbonIds) {
            var ribbons = [[${ribbons}]];
            var objs = [];
            for (var i = 0; i < ribbons.length; i++) {
                if(ribbons[i].id == ribbonIds){
                    objs.push(ribbons[i].inventories);
                    break;
                }
            }
            data = objs[0];
        }

        //原膜添加
        function addColumn() {
            //获得到合并材料的id
            var ribbonIds = $("#ribbonId").val();
            if(ribbonIds==null || ribbonIds==""){
                layer.alert("请选择合并材料");
                return false;
            }
            this.getData(ribbonIds);
            var table1 = $("#" + table.options.id).bootstrapTable('getData');
            var count = table1.length;
            // sub.editColumn();

            $("#" + table.options.id).bootstrapTable('insertRow', {
                index: count,
                row: {
                    index: $.table.serialNumber(count),
                    id:"",
                    factoryName: "",
                    materialsName: "",
                    color: "",
                    mDate: "",
                }
            });
            for (var i = 0; i < count; i++) {
                var obj = table1[i];
                $("#select"+i+" option[value= " + obj.id + "]").prop("selected",true);
            }
        }
        //原膜选择映射
        function getInput(val,index) {
            var table1 = $("#" + table.options.id).bootstrapTable('getData');
            var count = table1.length;
            var ids = table1.map(function (item) {
                if(item.index!=index+1){
                    return item.id;
                }
            }).join(',');
            if(val!="" && ids.indexOf(val)!=-1){
                layer.alert("不可重复选择");
                $("#select"+index+" option[value= '']").prop("selected",true);
                return false;
            }
            var obj = {};
            for (var i = 0; i < data.length; i++) {
                if(val == data[i].id){
                    obj = data[i];
                    break;
                }
            }
            $("#" + table.options.id).bootstrapTable('updateRow', {
                index: index,
                row: {
                    index: $.table.serialNumber(index),
                    id:obj.id,
                    factoryName: obj.factoryName,
                    materialsName: obj.materialsName,
                    color: obj.color,
                    mDate: obj.mDate,
                }
            });
            for (var i = 0; i < count; i++) {
                var obj = table1[i];
                $("#select"+i+" option[value= " + obj.id + "]").prop("selected",true);
            }
        }
        //原膜删除
        function delColumn(column) {
            // sub.editColumn();
            var subColumn = $.common.isEmpty(column) ? "index" : column;
            var ids = $.table.selectColumns(subColumn);
            if (ids.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            $("#" + table.options.id).bootstrapTable('remove', { field: subColumn, values: ids });
            var table1 = $("#" + table.options.id).bootstrapTable('getData');
            var count = table1.length;
            for (var i = 0; i < count; i++) {
                var obj = table1[i];
                $("#select"+i+" option[value= " + obj.id + "]").prop("selected",true);
            }
        }
        function getSurface(val) {
            var arrays = [[${ribbons}]];
            for (var i = 0; i < arrays.length; i++) {
                if(val == arrays[i].id){
                    var sub = "-";
                    if(arrays[i].surface == 1){
                        sub = "内";
                    }else if(arrays[i].surface == 2){
                        sub = "外";
                    }else if(arrays[i].surface == 3){
                        sub = "内+外";
                    }
                    $("#surface").val(arrays[i].surface);
                    $("#surfaceName").val(sub);
                }
            }
        }
    </script>
</body>
</html>

<script id="goodsTypeTpl" type="text/x-jquery-tmpl">
<div>
    <select class='form-control selectList' id="select${index}" style="width:100px" onchange="getInput(this.value,${index})">
        <option value="">所有</option>
        {{each(i,obj) list}}
            <option value="${obj.id}">${obj.number}</option>
        {{/each}}
    </select>
</div>
</script>